package com.jp.knowledge.comm;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.anzewei.parallaxbacklayout.ParallaxHelper;
import com.jiayen.dialog.LoadingDialog;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.LoginSecondActivity;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.command.CommandInvoker;
import com.jp.knowledge.command.CommandReceiver;
import com.jp.knowledge.command.implement.GangWeiZhiShiPaiXu;
import com.jp.knowledge.dialog.BgLoadingDialog;
import com.jp.knowledge.my.activity.UserSettingActivity;
import com.jp.knowledge.util.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import org.xutils.x;

/**
 * Created by jiayen on 16/9/26.
 * activity基础类
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected RelativeLayout headLayout;
    protected TextView topName;
    protected TextView topDesc;
    protected ImageView leftIcon;
    protected ImageView rightIcon;
    protected TextView rightBtn;
    protected View contentView;

    protected JpApplication application;
    protected Context mContext;
    protected LoadingDialog loading;//加载进度
    protected BgLoadingDialog bgLoadingDialog;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = getLayoutInflater().inflate(getResourceId(),null);
        contentView.setBackgroundResource(R.color.white);
        setContentView(contentView);

        x.view().inject(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        application = (JpApplication) getApplication();
        application.addActivity(this);
        this.mContext = this;
        loading = new LoadingDialog(mContext);
        bgLoadingDialog = new BgLoadingDialog(mContext);

        PushAgent.getInstance(this).onAppStart();       // 友盟推送需要加上这一行

        initHead();
        init();
        initUmen();
    }

    protected void  initUmen(){
        MobclickAgent.setDebugMode(true);
        // SDK在统计Fragment时，需要关闭Activity自带的页面统计，
        // 然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
        MobclickAgent.openActivityDurationTrack(true);
        // MobclickAgent.setAutoLocation(true);
        // MobclickAgent.setSessionContinueMillis(1000);
        // MobclickAgent.startWithConfigure(
        // new UMAnalyticsConfig(context, "4f83c5d852701564c0000011", "Umeng",
        // EScenarioType.E_UM_NORMAL));
        MobclickAgent.setScenarioType(mContext, MobclickAgent.EScenarioType.E_UM_NORMAL);
    }


    protected View getContentView(){
        return contentView;
    }


    /***
     * 显示加载进度
     */
    public  void showLoading(){
        loading.show();
    }

    protected void showBgLoadingDialog(){
        bgLoadingDialog.show();
    }

    /**
     * 隐藏加载进度
     */
    public  void cancleLoading(){
        loading.cancel();
    }

    protected void cancleBgLoadingDialog(){
        bgLoadingDialog.dismiss();
    }

    /**
     * 初始化导航栏
     */
    private void initHead() {
        headLayout= (RelativeLayout) findViewById(R.id.top);
        topName = (TextView) findViewById(R.id.module_name);
        topDesc = (TextView) findViewById(R.id.module_desc);
        leftIcon = (ImageView) findViewById(R.id.icon_left);
        rightIcon = (ImageView) findViewById(R.id.icon_serach);
        rightBtn = (TextView) findViewById(R.id.right_bn);
        if(topName != null) {
//            topName.setMaxWidth((int) (getResources().getDisplayMetrics().widthPixels * 0.5));
            topName.setMaxEms(12);
        }
    }

    /**
     * 获取主资源id
     * @return
     */
    protected abstract int getResourceId();

    /**
     * 初始化
     */
    protected  abstract  void init();

    /* 避免重复点击多次打开 activity */
    private long avoidRepeatTime;
    @Override
    public void startActivityForResult(final Intent intent, final int requestCode, final Bundle options) {
        long currentTime = System.currentTimeMillis();
        if(currentTime - avoidRepeatTime < 800) {
            avoidRepeatTime = currentTime;
            return;
        }
        avoidRepeatTime = currentTime;
        super.startActivityForResult(intent, requestCode, options);
    }

    @Override
    public void startActivity(Intent intent) {
        startActivity(intent, null);
    }

    @Override
    public void startActivity(Intent intent, Bundle options) {
        startActivityForResult(intent, -1);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void finish() {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        imm.hideSoftInputFromWindow(contentView.getWindowToken(), 0);
        super.finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
